﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace MapRoom
{
    public partial class MainWindow : Form
    {
        private MapModel _mapModel;
    
        public MainWindow()
        {
            InitializeComponent();
            _mapModel = new MapModel( );
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void openPortToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void scanForRobotToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if( !SerialPortControl.isScanning )
            {
                RobotScanDialog myRobotScanDialog = new RobotScanDialog( );
                myRobotScanDialog.Show( );
                SerialPortControl.startRobotScan( myRobotScanDialog, _mapModel );
            }
            else
            {
                MessageBox.Show( "Please wait for current scan to finish before continuing.", "Scan in progress",
                    MessageBoxButtons.OK, MessageBoxIcon.Error );
            }
        }

        private void MainWindow_Paint(object sender, PaintEventArgs e)
        {
            Graphics graphicsObject = e.Graphics;
            Pen wallPen = new Pen( Color.PeachPuff );
            Pen doorPen = new Pen( Color.BlueViolet );
            Pen robotPen = new Pen( Color.Red );
            SolidBrush wordBrush = new SolidBrush( Color.LawnGreen );

            if( !SerialPortControl.haveFoundRobot( ) )
            {
                graphicsObject.Clear( Color.Black );
                return;
            }

            RobotPOIData myData = _mapModel.getRobotPOIData( );
            if( myData == null )
            {
                return;
            }

            LinkedList<MapPoint> myMapPoints = new LinkedList<MapPoint>( _mapModel.getMapPoints( ) );
            if( myMapPoints == null )
            {
                return;
            }

            int pixelsPerFoot = 20;
            graphicsObject.DrawArc( robotPen, myData.robotLocationX * pixelsPerFoot + 30,
                myData.robotLocationY * pixelsPerFoot + 30, 10, 10, 0, 360 );
            graphicsObject.DrawLine( robotPen, myData.robotLocationX * pixelsPerFoot + 35,
                myData.robotLocationY * pixelsPerFoot + 35, ( myData.robotLocationX * pixelsPerFoot + 35 ) +
                ( int )( Math.Cos( ( myData.robotAngle - 90 ) * 0.01745 ) * 10.0 ),
                ( myData.robotLocationY * pixelsPerFoot + 35 ) +
                ( int )( Math.Sin( ( myData.robotAngle - 90 ) * 0.01745 ) * 10.0 ) );
            
            foreach( MapPoint mapPoint in myMapPoints )
            {
                graphicsObject.DrawRectangle( wallPen, myData.robotLocationX * pixelsPerFoot + 35 + ( float )mapPoint.getY( )/5,
                    myData.robotLocationY * pixelsPerFoot + 35 + ( float )mapPoint.getX( )/5, 2, 2 );
            }
            //graphicsObject.DrawRectangle( wallPen, 30, 30, myData.roomWidth * pixelsPerFoot + 10, 
            //    myData.roomHeight * pixelsPerFoot + 10 );
            //graphicsObject.DrawRectangle( doorPen, myData.doorX * pixelsPerFoot + 30, myData.doorY * pixelsPerFoot + 30, 
            //    myData.doorWidth * pixelsPerFoot + 1, myData.doorHeight * pixelsPerFoot + 1 );
            
            //Font myFont = new Font( "Arial", 12 );
            //graphicsObject.DrawString( String.Format( "({0},{1})", myData.roomWidth, myData.roomHeight ), 
            //    myFont, wordBrush, 40, myData.roomHeight * pixelsPerFoot + 45 );
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Invalidate( );
        }

        private void stopRobotToolStripMenuItem_Click( object sender, EventArgs e )
        {
            if( SerialPortControl.haveFoundRobot( ) )
            {
                SerialPortControl.port.Write( "st" );
            }
        }

        private void startRobotToolStripMenuItem_Click( object sender, EventArgs e )
        {
            if( SerialPortControl.haveFoundRobot( ) )
            {
                SerialPortControl.port.Write( "go" );
            }
        }

        private void resetRoomDataToolStripMenuItem_Click( object sender, EventArgs e )
        {
            _mapModel.resetData( );
        }
    }
}
